package com.wdemo.memory;

import com.wdemo.enums.ChatTypeEnum;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ChatMemoryManager
 * @Description 聊天内存管理器
 * @Author WDEMO
 * @Date 2025/8/31 0:38
 */
@Slf4j
@Component
public class ChatMemoryManager {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static  final Map<ChatTypeEnum, ChatMemory> chatMemoryMap=new HashMap<>();

//    public ChatMemoryManager() {
//        log.info("初始化聊天内存管理器");
//        // 初始化不同类型的聊天内存
//        for (ChatTypeEnum type : ChatTypeEnum.values()) {
//            chatMemoryMap.put(type, new RedisChatMemory(type.getType(),stringRedisTemplate));
//        }
//        log.info("初始化聊天内存管理器完成");
//    }

    @PostConstruct
    public void init() {
        log.info("初始化聊天内存管理器");
        // 初始化不同类型的聊天内存
        for (ChatTypeEnum type : ChatTypeEnum.values()) {
            // 传递 StringRedisTemplate 给 RedisChatMemory
            chatMemoryMap.put(type, new RedisChatMemory(type.getType()+":", stringRedisTemplate));
        }
        log.info("初始化聊天内存管理器完成");
    }
    public ChatMemory getChatMemory(ChatTypeEnum chatType) {
        return chatMemoryMap.get(chatType);
    }

    public ChatMemory getChatMemory(String chatType) {
        return chatMemoryMap.get(ChatTypeEnum.fromString(chatType));
    }
}
